home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk70 / whereis2 / whereis.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  8KB  |  289 lines

  1. /* Find the file whose name matches that passed as first argument.
  2.  * Print the filename and its volume:path to the screen
  3.  * Some cosmetic modifications made to this code to report the
  4.  * size and last change date/time of the file as it is listed.
  5.  * Changes marked with KDP (very minor changes) - Ken Presser.
  6.  */
  7. #include <exec/types.h>
  8. #include <devices/keymap.h>
  9. #include <intuition/intuition.h>
  10. #include <libraries/dosextens.h>
  11. #include <stdio.h>
  12. #include <ctype.h>
  13.  
  14. #define BELL 0x07
  15. /*
  16.  *                 F U N C T I O N     D E C L A R A T I O N S
  17.  */
  18. extern struct FileLock * Lock();
  19.  
  20. BOOL ifoundit;
  21. /* home grown subrs: */
  22. void close_things();
  23.  
  24. /*
  25.  *                  M A I N     P R O G R A M     M O D U L E
  26.  */
  27. void
  28. main( argc,argv)
  29. int argc;
  30. char * argv[];
  31. {
  32.     void scan_directory ();
  33.     struct FileLock * Lock ();
  34.     void UnLock ();
  35.     char * strcpy ();
  36.  
  37.     struct FileLock * dir;
  38.     char path[130];            /* hold volume name */
  39.     char devname[32];
  40.     char searchfile[32];
  41.     char * ch;
  42.     UBYTE i;
  43.  
  44.     if ((argc > 3) || (argc == 1))
  45.         printf ("usage: whereis <filename> <devname>\n");
  46.     else
  47.         {
  48.         ifoundit = FALSE;
  49.         /* devname not specified */
  50.         if (argc == 2)
  51.             strcpy(devname,":");                /*KDP*/
  52.         else
  53.             {
  54.             for (i=0;i<32;i++)
  55.                 devname[i] = '\0';
  56.             strncpy (devname,argv[2],31);
  57.             }
  58.         strcpy (searchfile, argv[1]);
  59.         /* upcase input string, simplify matching process */
  60.         for (ch=searchfile; *ch != '\0'; ch++)
  61.             *ch = toupper (*ch);
  62.         /* Get Lock on disk */
  63.         if ( (dir = Lock (devname,ACCESS_READ)) == NULL)
  64.             {
  65.             printf ("Could not obtain lock on device %s\n",
  66.                 devname);
  67.             printf ("Then Die Ceasar\n");
  68.             close_things ();
  69.             exit (5);
  70.             }
  71.         else
  72.             strcpy (path, devname);
  73.         /* DO IT TO IT! */
  74.         scan_directory (searchfile,path);
  75.         }
  76.     close_things ();
  77. } /* end main */
  78.  
  79. /*
  80. .page.index close_index
  81. Do whatever final clean up is needed to leave the program.
  82.  */
  83. void
  84. close_things()
  85. {
  86.     if (!ifoundit)
  87.         printf ("Sorry, couldn't find it.\n");
  88.     return;
  89. } /* end close_things */
  90.  
  91.  
  92. /*
  93. .page.index scan_directory
  94.  */
  95. void
  96. scan_directory (searchfile,path)
  97. char * searchfile;
  98. char * path;
  99.     {
  100.     char * strcpy ();
  101.     void UnLock ();
  102.     struct FileLock * dir;
  103.     struct FileInfoBlock * fb;
  104.     char * adddir ();
  105.  
  106.     /* WE GOTTA BE STINGY WITH LOCAL STORAGE, THIS IS RECURSING!
  107.      * CAN'T EAT TOO MUCH STACK.
  108.      */
  109. #    define MAXSUB 100
  110.     char * subdir [MAXSUB];    /* Pointer area for Sub-Directories */
  111.     char pathname[130];    /* Pointers to AmigaDOS sub-directories */
  112.     UBYTE countdir,indexdir,i;
  113.     char testname[32],prntname[32];                /*KDP*/
  114.  
  115.     /* Get Lock on this directory */
  116.     if ( (dir = Lock (path,ACCESS_READ)) == NULL)
  117.         {
  118.         printf ("%cCould not obtain lock on directory %s\n",
  119.             BELL, path);
  120.         printf ("Then Die Ceasar\n");
  121.         close_things ();
  122.         exit (4);
  123.         }
  124.  
  125.     /* Examine lock and obtain FileInfoBlock */
  126.     fb=(struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock),0);
  127.     if (!Examine (dir,fb))
  128.         {
  129.         printf ("%cCouldn't Examine files assoc with lock\n",BELL);
  130.         printf ("Then Die Ceasar\n");
  131.         UnLock (dir);
  132.         FreeMem ((char *)fb, sizeof (struct FileInfoBlock));
  133.         close_things ();
  134.         exit (4);
  135.         }
  136.     /* we now have dir and fb set up, 
  137.      * scan all files at this level 
  138.      * (remembering subdirectories)
  139.      */
  140.     countdir = 0;
  141.     while ((ExNext(dir,fb),IoErr() != ERROR_NO_MORE_ENTRIES) 
  142.         && (countdir <= MAXSUB)
  143.         ) 
  144.         {
  145.         if (fb->fib_DirEntryType > 0)
  146.             {
  147.             /* we have a subdirectory here... */
  148.             if (countdir < MAXSUB)
  149.                 subdir[countdir] = 
  150.                     adddir (fb->fib_FileName);
  151.             else
  152.                 printf ("%cToo few subdirectory slots\n",
  153.                     BELL);
  154.             countdir++;
  155.             }
  156.         else
  157.             {
  158.             /* compare fb->filename with searchfile */
  159.             for (i=0;i<32;i++)
  160.                 testname[i] = prntname[i] = '\0';        /*KDP*/
  161.             strncpy (prntname,fb->fib_FileName, 32);    /*KDP*/
  162.             testname[31] = prntname[31] = '\0';            /*KDP*/
  163.             for (i = 0; (prntname[i] != '\0')&&(i<32); ++i)        /*KDP*/
  164.                 testname[i] = toupper (prntname[i]);    /*KDP*/
  165. #            if 0
  166.             printf ("%s =?= %s\n",testname, searchfile);
  167. #            endif
  168.             if (strcmp (testname,searchfile)==0)
  169.                 {
  170.                 printf ("%s/%s\tSize=%ld\t",            /*KDP*/
  171.                     path,prntname,fb->fib_Size);        /*KDP*/
  172.                 datetime(fb->fib_Date.ds_Days,            /*KDP*/
  173.                     fb->fib_Date.ds_Minute);            /*KDP*/
  174.                 printf("\n");                            /*KDP*/
  175.                 ifoundit = TRUE;
  176.                 }
  177.             }
  178.         } /* end while */
  179.     /* return these now, we're done, thank you. */
  180.     UnLock (dir);
  181.     FreeMem ((char *)fb, sizeof (struct FileInfoBlock));
  182.  
  183.     /* finished with this level, try one level down */
  184.     indexdir = 0;
  185.     while (indexdir < countdir)
  186.         {
  187.         strcpy (pathname,path);
  188.         /* if no path delimiter, add one */
  189.         if (       (path[strlen(path)-1] != ':') 
  190.             && (path[strlen(path)-1] != '/') 
  191.             && (strlen(path) > 0))
  192.                   strcat (pathname,"/");
  193.         strcat (pathname,subdir[indexdir]);
  194.  
  195.         scan_directory (searchfile,pathname);
  196.  
  197.         /* and pick up litter left by adddir() */
  198.         FreeMem ((char *)subdir[indexdir],
  199.             strlen(subdir[indexdir])+1);
  200.         subdir[indexdir] = NULL;
  201.         indexdir++;
  202.         }
  203.  
  204.     return;
  205. }    /* end scan_directory */
  206.  
  207. /*
  208. .page.index adddir
  209.  ----------------------------------------------------------------------------
  210.     Allocate some memory and then copy string into it.
  211.  */
  212. char * adddir (string)
  213. char * string;
  214. {
  215.     char * nameadd;    /* Address of the directory name */
  216.  
  217.     nameadd = (char *) AllocMem (strlen(string)+1,0);
  218.     if (nameadd == NULL)
  219.         {
  220.         printf ("Not Enough Memory for Directories!!!\n");
  221.         /* run away! run away! */
  222.         close_things ();
  223.         exit (7);
  224.         }
  225.     strcpy (nameadd, string);
  226.  
  227.     /* return address of string you just alloated. */
  228.     return (nameadd);
  229.  
  230. }    /*end adddir*/
  231.  
  232. /*
  233. ** The following slightly modified to act as a subroutine and
  234. ** to be less verbose - KDP
  235. */
  236.  
  237. /* A non-recursive date calculator for the Amiga by John A. Hodgson.
  238.                       May be distribute freely
  239.    The algorithm requires a correction for multiples of 59 years from
  240.    the base date (1978), non - leap years falling on a new century and
  241.    dates more than 365 years from the base date. As much as I like the
  242.    Amiga, I didn't think it would be around (functionally) by then, so
  243.    didn't put these in.          --- Enjoy ---                         */
  244.  
  245. int dpm[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  246.  
  247. datetime(ddate,minutes)
  248. long    ddate,minutes;
  249. {
  250.     int        i, years, lpyears, leaprem, days, hour, minute;
  251.  
  252.     years =  ddate / 365;           /* approx number of years past  */
  253.     lpyears  =  (years+2) / 4;      /* approx number of leap years  */
  254.     leaprem  =  (years+2) % 4;
  255.     if (!(leaprem)) {               /* if we are in a leap year     */
  256.         lpyears  =  lpyears - 1;    /* don't correct days           */
  257.         dpm[1]   =  29;             /* but fix February             */
  258.     }
  259.     days  =    1 + ddate % 365 - lpyears;  /* 1 + for Jan 1, 1978    */
  260.                                      /* approx correction to days    */
  261.     if (days <= 0) {                 /* check if we overflowed a year*/
  262.         years =  years - 1;          /* and correct years            */
  263.         days  =  days + 365;         /* and days                     */
  264.         if (!(leaprem)) {            /* if we backed off a leap year */
  265.             days = days - 1;         /* we lose the extra day        */
  266.         }
  267.         dpm[1]   =  28;              /* restore February             */
  268.         if (leaprem == 1) {          /* if overflowed to a leap year */
  269.             dpm[1]   =  29;          /* fix February                 */
  270.         }
  271.         days  =  days + 1;           /* and days                     */
  272.     }
  273.  
  274.     years =  years + 1978;
  275.  
  276.     i  =  0;
  277.     while (days > 0) {
  278.         days = days - dpm[i++];      /* figure which month           */
  279.     }
  280.     days  =  days + dpm[--i];        /* and day                      */
  281.  
  282.     hour = minutes / 60;
  283.     minute = minutes % 60;
  284.  
  285.     printf ("%2d/%02d/%04d at %2d:%02d",(i+1),days,years,hour,minute);
  286.  
  287.     return 0;
  288. }
  289.